/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.previsor.workflow.control;

import de.previsor.workflow.model.TempData;
import de.previsor.workflow.control.DBConnect;
import de.previsor.workflow.control.helper.InitData;
import java.security.NoSuchAlgorithmException;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

/**
 *
 * @author robertkeller
 */
public class ProfilController {
    @FXML private TextField inp_bereich;
    @FXML private TextField inp_recht;
    @FXML private TextField inp_email;
    @FXML private TextField inp_name;
    @FXML private Button btn_save;
    
    DBConnect c = new DBConnect();
    
    String Vorname;
    
    int UID;
    String UName;
    String UMail;
    String UBereich;
    String URecht;
    
    String OUName;
    String OUMail;
    String OUBereich;
    String OURecht;
    
    @FXML 
    private void edit(ActionEvent event)
    {
        toggleEdit(true);
        UID = c.getUserID(inp_email.getText());
        OUName = inp_name.getText();
        OUMail = inp_email.getText();
        OUBereich = inp_bereich.getText();
        OURecht = inp_recht.getText();
        String[] UData = new String[6];
        UData = c.getUserData(UID);
        Vorname = UData[1];
    }
    
    
    @FXML
    private void saveC(ActionEvent event) throws NoSuchAlgorithmException
    {
        
        UName = inp_name.getText();
        UMail = inp_email.getText();
        UBereich = inp_bereich.getText();
        URecht = inp_recht.getText();
        
        if("".equals(UName))
        {
            UName = OUName;
        }
        if("".equals(UMail))
        {
            UName = OUMail;
        }
        if("".equals(UBereich))
        {
            UName = OUBereich;
        }
        if("".equals(URecht))
        {
            UName = OURecht;
        }
        
        if("".equals(OUMail))
        {
            if("".equals(UName) || "".equals(UMail) || "".equals(UBereich) || "".equals(URecht))
            {
                System.out.println("Fehlerhafte Daten!");
            }
            else
            {
            if(c.addUser("previsor",UName,UName,UMail,UBereich,URecht))
            {
                System.out.println("hinzugefügt...");
            }   
            }
        }
        else
        {
        c.updateUser(UID, Vorname, UName, UMail, UBereich, URecht);
        }
        toggleEdit(false);
        InitData.startInit(TempData.getBenutzer());
        System.out.println("speichern...");
    }
    
    
    private void toggleEdit(boolean editable)
    {
        if(editable == true)
        {
        this.inp_name.setEditable(true);
        this.inp_email.setEditable(true);
        this.inp_bereich.setEditable(true);
        this.inp_recht.setEditable(true);
        this.inp_name.setDisable(false);
        this.inp_email.setDisable(false);
        this.inp_bereich.setDisable(false);
        this.inp_recht.setDisable(false);
        btn_save.setVisible(true);
        }
        
        else if(editable == false)
        {
        this.inp_name.setEditable(false);
        this.inp_email.setEditable(false);
        this.inp_bereich.setEditable(false);
        this.inp_recht.setEditable(false);
        this.inp_name.setDisable(true);
        this.inp_email.setDisable(true);
        this.inp_bereich.setDisable(true);
        this.inp_recht.setDisable(true); 
        btn_save.setVisible(false);
        }
    }
    
}
